
//
//  ZZYTitleScrollView.m
//  ZZYHeaderView
//
//  Created by 张振宇 on 15/10/29.
//  Copyright © 2015年 张振宇. All rights reserved.
//

#import "ZZYTitleScrollView.h"

@interface ZZYTitleScrollView()<UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *buttonArray;
@property (nonatomic, strong) NSMutableArray *titlesArray;

@end

@implementation ZZYTitleScrollView
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate     = self;
        self.buttonArray  = [NSMutableArray array];
        self.titlesArray  = [NSMutableArray array];
        self.backgroundColor = [UIColor clearColor];
        self.buttonWidth  = 40.0f;
    }
    return self;
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        self.delegate     = self;
        self.buttonArray  = [NSMutableArray array];
        self.titlesArray  = [NSMutableArray array];
        self.backgroundColor = [UIColor clearColor];
        self.buttonWidth  = 40.0f;
    }
    return self;
}
- (void)setupWithTitleArray:(NSArray *)array{
    self.titlesArray = [NSMutableArray arrayWithArray:array];
    self.buttonArray = [NSMutableArray array];
    self.contentSize = CGSizeMake(self.buttonWidth * (array.count+1), self.frame.size.height);
    self.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i < self.titlesArray.count; i++) {
        [self initButtonsWithTag:i];
    }
    [self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    
    //初次设定button，默认第一个按钮为选中状态
    //[self clickButton:(UIButton *)[self.buttonArray objectAtIndex:0]];
    //[self setButtonStatusWithOffsetX:self.contentOffset.x];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"]) {
       // [self setButtonStatusWithOffsetX:self.contentOffset.x];
    }
}

/*
 更改button的效果请修改这个方法
 */
-(void)initButtonsWithTag:(int)tag
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake((tag)*self.buttonWidth, 0, self.buttonWidth, self.frame.size.height);
    button.tag = tag;
    [button setFont:[UIFont systemFontOfSize:14]];
    [button setTitle:[self.titlesArray objectAtIndex:tag] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor colorWithWhite:0.000 alpha:0.846] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height -2, self.buttonWidth, 1)];
    view.backgroundColor = [UIColor redColor];
    [button addSubview:view];
    [self.buttonArray addObject:button];
    
    [self addSubview:button];
    [self changeViewWithTag:0];
//    CGPoint center = button.center;
//    center.x = (button.tag+1)*self.buttonWidth + self.buttonWidth/2.0f;
//    button.center = center;
    
}

-(void)clickButton:(UIButton *)button
{
    CGFloat left = (self.buttonWidth*button.tag);
    [self setButtonStatusWithOffsetX:left];
    
    if (_titlesdelegate && [_titlesdelegate respondsToSelector:@selector(scrollviewShouldScollByTitleScollview:)]) {
        [_titlesdelegate scrollviewShouldScollByTitleScollview:button.tag];
    }
}
- (void)changeViewWithTag:(NSInteger)tag{
    [self setButtonStatusWithOffsetX:tag * self.buttonWidth];
}

//核心内容
-(void)setButtonStatusWithOffsetX:(CGFloat)offsetX
{
    if (offsetX < 0) {
        return;
    }
    
    if (offsetX >= (self.contentSize.width - self.buttonWidth)) {
        return;
    }
    
    int tempTag = (offsetX/self.buttonWidth);
    
    if (tempTag > self.titlesArray.count - 1) {
        return;
    }
    
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.subviews[0].backgroundColor = [UIColor clearColor];
    }
    
    UIButton *buttonleft = [self.buttonArray objectAtIndex:tempTag];
    buttonleft.subviews[0].backgroundColor = [UIColor redColor];
    [buttonleft setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}





-(void)dealloc
{
    self.titlesdelegate = nil;
    self.delegate = nil;
    [self removeObserver:self forKeyPath:@"contentOffset"];
}
@end
